package hht.dragon.blog.mapper;

import hht.dragon.blog.model.Type;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 类型表数据库操作.
 * User: huang
 * Date: 18-6-22
 */
@Repository
public interface TypeMapper {

    /**
     * 查询所有类型
     * @return
     */
    @Select("select * from type")
    @Results({
            @Result(id = true, property = "typeId", column = "type_id"),
            @Result(property = "size", column = "type_id",
            one = @One(select = "hht.dragon.blog.mapper.TypeMapper.getTypeArticleSize"))
    })
    List<Type> getAllType();

    /**
     * 获取文章的所属类型.
     * @param articleId 文章id
     * @return
     */
    @Select("select * from type where type_id in (select type_id from type_article where article_id = #{articleId})")
    List<Type> getTypesByArticle(@Param("articleId") int articleId);

    @Select("select count(*) from type_article where type_id = #{typeId}")
    int getTypeArticleSize(int typeId);

}
